package eks.diverse;

import android.app.Activity;
import android.app.SearchManager;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.view.View;
import android.view.View.OnClickListener;
import android.os.Bundle;
import android.provider.Settings.Secure;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TableLayout;

/**
 * 
 * @author Jacob Nordfalk
 */
public class BenytIntents extends Activity implements OnClickListener {

  EditText editText;
  Button sendSms, sendEmail, websøgning;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    TableLayout tl=new TableLayout(this);
    editText=new EditText(this);
    editText.setText("Skriv tekst her");
    tl.addView(editText);

    sendSms=new Button(this);
    sendSms.setText("Send SMS");
    tl.addView(sendSms);

    sendEmail=new Button(this);
    sendEmail.setText("Send email");
    tl.addView(sendEmail);

    websøgning=new Button(this);
    websøgning.setText("Websøgning");
    tl.addView(websøgning);

    setContentView(tl);

    sendSms.setOnClickListener(this);
    sendEmail.setOnClickListener(this);
    websøgning.setOnClickListener(this);
  }

  public void onClick(View hvadBlevDerKlikketPå) {
    if (hvadBlevDerKlikketPå == sendSms)
    {
        åbnSendSms("26206512", editText.getText().toString());
    } else
    if (hvadBlevDerKlikketPå == sendEmail)
    {
        åbnSendEmail("jacob.nordfalk@gmail.com", editText.getText().toString(), lavModelinfo());
    } else
    if (hvadBlevDerKlikketPå == websøgning)
    {
        websøgning(editText.getText().toString());
    }
  }

	/** Åbner et SMS-vindue og lader brugeren sende SMS'en */
	public void åbnSendSms(String nummer, String  besked) {
		// Kilde: http://andmobidev.blogspot.com/2010/01/launching-smsmessages-activity-using.html
    Intent intent=new Intent(Intent.ACTION_VIEW);
    //Intent intent=new Intent(Intent.ACTION_VIEW, Uri.parse("sms:"+number));
    intent.setType("vnd.android-dir/mms-sms");
    intent.putExtra("sms_body", besked);
    intent.putExtra("address", nummer);
    startActivity(intent);
	}

  public void websøgning(String søgestreng) {
    Intent searchGivenText=new Intent(Intent.ACTION_WEB_SEARCH);
    searchGivenText.putExtra(SearchManager.QUERY, søgestreng);
    startActivity(searchGivenText);
  }

  public void åbnUrl(String url) {
    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
    startActivity(intent);
  }

  void åbnSendEmail(String modtager, String emne, String txt) {
      Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
      emailIntent.setType("plain/text");
      emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{modtager});
      emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, emne);
      emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, txt);
      startActivity(Intent.createChooser(emailIntent, "Send mail..."));
  }

  public String lavModelinfo() {
    String version = null;
    try {
      PackageInfo pi=getPackageManager().getPackageInfo(getPackageName(), PackageManager.GET_ACTIVITIES);
      version = pi.versionName;
    } catch (Exception e) { e.printStackTrace(); }
    return "\nProgram: "+getPackageName()+" version " + version
      +"\nTelefonmodel: "+Build.MODEL
      +"\n"+Build.PRODUCT
      +"\nAndroid v"+Build.VERSION.RELEASE
      +"\nsdk: r"+Build.VERSION.SDK  // SDK_INT kommer først i Androd 1.6
      +"\nAndroid ID: "+Secure.getString(getContentResolver(), Secure.ANDROID_ID);
  }
}
